//
//  CTVDownloadController.m
//  Channltv
//
//  Progress bar with percent and download time
//
//  Created by Bruno  Keymolen on 22/07/07.
//  Copyright 2007 channl.tv. All rights reserved.
//

#import "CTVDownloadController.h"
#import "CTVConfig.h"

@implementation CTVDownloadController

- (id) initWithScene: (BRRenderScene *) scene{
    self = [super initWithScene: scene];

	//Create and add the layers
    m_mainlayer = [[BRRenderLayer alloc] initWithScene: scene];
    m_progresslayer = [[BRProgressBarWidget alloc] initWithScene: scene];
	m_textlayer = [[BRTextControl alloc] initWithScene: scene];
	m_mbLayer = [[BRTextControl alloc] initWithScene: scene];
	m_timeLeftLayer = [[BRTextControl alloc] initWithScene: scene];

    [m_mainlayer addSublayer: m_progresslayer];
	[m_mainlayer addSublayer: [m_textlayer layer] ];
	[m_mainlayer addSublayer: [m_mbLayer layer] ];
	[m_mainlayer addSublayer: [m_timeLeftLayer layer] ];


	[m_textlayer setTextAttributes:[[BRThemeInfo sharedTheme] menuItemTextAttributes]];
	[m_timeLeftLayer setTextAttributes:[[BRThemeInfo sharedTheme] paragraphTextAttributes]];
	[m_mbLayer setTextAttributes:[[BRThemeInfo sharedTheme] paragraphTextAttributes]];
	
    // defaults

	return self;
}


- (void) dealloc{
    [m_mainlayer release];
    [m_progresslayer release];
	[m_textlayer release];
	[m_text release];
	[m_mbLayer release];
	[m_timeLeftLayer release];
    [super dealloc];
}

-(void) redraw{
	[[m_textlayer scene] renderScene];
	[[m_progresslayer scene] renderScene];
	[[m_mainlayer scene] renderScene];
}


- (BRRenderLayer *) layer
{
    return ( m_mainlayer );
}

- (void) setFrame: (NSRect) frame{
    [super setFrame: frame];

	[self _layout];
}





- (void) setText:(NSString*)text{
	[text retain];
	[m_text release];
	m_text = text;
	[m_textlayer setMaxLines:2];
	
	//Center 
	
	[m_textlayer setText:m_text];
}

- (void) setPercent:(int)percent{
	m_percent = percent;
	[m_progresslayer setPercentage:percent];
	
}


-(void)setTimeLeft:(NSString*)timeLeft{
	[m_timeLeftLayer setText:timeLeft];
	[self _layout];
}

-(void)setMB:(NSString*)mb{
	[m_mbLayer setText:mb];
	[self _layout];
}







-(void)_layout{
	NSRect frame = [super frame];
	
	[m_progresslayer setPercentage:m_percent]; 
	[m_textlayer setText:m_text];
	
	//Progress bar - full width, y=20% , h=40% 
    NSRect progresFrame = NSZeroRect;
    progresFrame.size.width = frame.size.width;
    progresFrame.size.height = ceilf( frame.size.height * 0.4f );
	progresFrame.origin.y = ceilf( frame.size.height * 0.2f );
    [m_progresslayer setFrame: progresFrame];
	
	//Title - center, y=60%, h=40%
	NSRect textFrame = NSZeroRect;
	NSSize textSize = [m_textlayer renderedSize];
	if(textSize.width > frame.size.width){
		textFrame.size.width = frame.size.width * 0.95f;
	}else{
		textFrame.size.width = textSize.width; 
    }
	textFrame.size.height = ceilf( frame.size.height * 0.4f );
	textFrame.origin.y = ceilf( frame.size.height * 0.6f );
	textFrame.origin.x = (frame.size.width - textFrame.size.width) * 0.5f;
    [m_textlayer setFrame: textFrame];

	//time left, x=0, y=0, h=20%
	NSRect tlFrame = NSZeroRect;
	tlFrame.origin.x = 0.0f;
	tlFrame.origin.y = 0.0f;
	tlFrame.size.height = ceilf( frame.size.height * 0.2f );
    [m_timeLeftLayer setFrame: tlFrame];
	
	//MB, right align, y=0, h=20%
	NSRect mbFrame = NSZeroRect;
	NSSize mbTextSize = [m_mbLayer renderedSize];
	mbFrame.origin.x = frame.size.width - mbTextSize.width;
	mbFrame.origin.y = 0.0f;
	mbFrame.size.height = ceilf( frame.size.height * 0.2f );
	[m_mbLayer setFrame:mbFrame];

}



@end
